package gitobject;

import fileoperation.FileReader;
import repository.Repository;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class Branch extends GitObject{
    protected ArrayList<GitObject> commitList = new ArrayList<>();
    protected Commit currentcommit = new Commit();

    public Branch(){}
  //  public Branch(String branch,Commit commit)
    public Branch(String branch,String commit){
   //     this.currentcommit = commit;
   //     this.commitList.add(commit);
        this.path = Repository.getGitDir() + File.separator + "refs/heads/";  // prepare the path to write()
        //分支储存路径为C:/Users/之靈/Desktop/Git\.jit\objects\refs/heads/
        this.file = new File(this.path + branch);
        this.key =  branch ;  // writeObject() destination
        this.value = commit + "\n"; // branch content is the commit hashcode
        if(file.exists()){
            try{
                this.value += FileReader.deserializeFileValue(file);
                writeObject();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /*
     *commit写入branch
     */
    public void Branch(File branch,String commitValue){
        String branchName = branch.getName();
        this.value += commitValue + "\n";
        try{
            this.value += FileReader.deserializeFileValue(branch);
            this.key = branchName ;  // writeObject() destination
            writeObject();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}